package com.mashibing.servicedriveruser.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.mashibing.constant.CommonStatusEnum;
import org.mashibing.constant.DriverCarConstant;
import org.mashibing.dto.ResponseResult;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.mashibing.dto.DriverCarBindingRelationship;
import com.mashibing.servicedriveruser.mapper.DriverCarBindingRelationshipMapper;
import com.mashibing.servicedriveruser.service.DriverCarBindingRelationshipService;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author: $dujunhua
 * @create: 2024/6/27 16:04
 * @description: 
 */
@Service
public class DriverCarBindingRelationshipServiceImpl extends ServiceImpl<DriverCarBindingRelationshipMapper, DriverCarBindingRelationship> implements DriverCarBindingRelationshipService{

    @Override
    public ResponseResult bind(DriverCarBindingRelationship driverCarBindingRelationship) {
        // 如果车辆和司机已绑定，则不允许重复绑定
        QueryWrapper<DriverCarBindingRelationship> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("driver_id",driverCarBindingRelationship.getDriverId());
        queryWrapper.eq("car_id",driverCarBindingRelationship.getCarId());
        queryWrapper.eq("bind_state", DriverCarConstant.DRIVER_CAR_BIND);
        Integer count = this.baseMapper.selectCount(queryWrapper);
        if (count > 0) {
            return ResponseResult.fail(CommonStatusEnum.DRIVER_CAR_BIND_EXITS.getCode(),CommonStatusEnum.DRIVER_CAR_BIND_EXITS.getValue());
        }
        // 如果司机已绑定，则不允许重复绑定
        queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("driver_id",driverCarBindingRelationship.getDriverId());
        queryWrapper.eq("bind_state", DriverCarConstant.DRIVER_CAR_BIND);
        count = this.baseMapper.selectCount(queryWrapper);
        if (count > 0) {
            return ResponseResult.fail(CommonStatusEnum.DRIVER_BIND_EXITS.getCode(),CommonStatusEnum.DRIVER_BIND_EXITS.getValue());
        }
        // 如果车辆已绑定，则不允许重复绑定
        queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("car_id",driverCarBindingRelationship.getCarId());
        queryWrapper.eq("bind_state", DriverCarConstant.DRIVER_CAR_BIND);
        count = this.baseMapper.selectCount(queryWrapper);
        if (count > 0) {
            return ResponseResult.fail(CommonStatusEnum.CAR_BIND_EXITS.getCode(),CommonStatusEnum.CAR_BIND_EXITS.getValue());
        }
        // 绑定司机和车辆
        driverCarBindingRelationship.setBindState(DriverCarConstant.DRIVER_CAR_BIND);
        driverCarBindingRelationship.setBindingTime(LocalDateTime.now());
        this.baseMapper.insert(driverCarBindingRelationship);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult unbind(DriverCarBindingRelationship driverCarBindingRelationship) {
        QueryWrapper<DriverCarBindingRelationship> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("driver_id",driverCarBindingRelationship.getDriverId());
        queryWrapper.eq("car_id",driverCarBindingRelationship.getCarId());
        queryWrapper.eq("bind_state", DriverCarConstant.DRIVER_CAR_BIND);
        DriverCarBindingRelationship carBindingRelationships = this.baseMapper.selectOne(queryWrapper);
        if (carBindingRelationships == null) {
            return ResponseResult.fail(CommonStatusEnum.DRIVER_CAR_BIND_NOT_EXITS.getCode(),CommonStatusEnum.DRIVER_CAR_BIND_NOT_EXITS.getValue());
        }
        carBindingRelationships.setBindState(DriverCarConstant.DRIVER_CAR_UNBIND);
        carBindingRelationships.setUnBindingTime(LocalDateTime.now());
        this.baseMapper.updateById(carBindingRelationships);
        return ResponseResult.success();
    }
}
